package com.iemp.result;

import com.iemp.config.consts.StateConsts;
import com.iemp.exception.entity.base.AbstractStateException;

import java.io.Serializable;

public class JsonResult<T> implements Serializable {

    private int state;
    private T data;
    private String message;

    private JsonResult(T data) {
        this.state = StateConsts.SUCCESS;
        this.data = data;
        this.message = "success";
    }

    private JsonResult(T data, String message) {
        this.state = StateConsts.SUCCESS;
        this.data = data;
        this.message = message;
    }

    private JsonResult(AbstractStateException exception) {
        this.state = exception.getState();
        this.data = null;
        this.message = exception.getMessage();
    }

    public static <T> JsonResult<T> create(AbstractStateException exception) {
        return new JsonResult<>(exception);
    }

    public static <T> JsonResult<T> create(T data, String message) {
        return new JsonResult<>(data, message);
    }

    public static <T> JsonResult<T> create(T data) {
        return new JsonResult<>(data);
    }

    public static JsonResult<String> create(String data) {
        return new JsonResult<String>(data);
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "JsonResult{" +
                "state=" + state +
                ", data=" + data +
                ", message='" + message + '\'' +
                '}';
    }
}
